package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.dao.MemberDao;
import com.itcast.pojo.Member;
import com.itcast.service.MemberService;
import com.itcast.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass =MemberService.class )
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

//    @Autowired
//    private BCryptPasswordEncoder passwordEncoder;

    /**
     * 查找会员
     * @param telephone 电话号码
     * @return 返回会员对象
     */
    @Override
    public Member fingMemberByTelephone(String telephone) {
        return memberDao.findMemberByPhoneNumber(telephone);
    }


    /**
     * 新增会员
     * @param member
     */
    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password!=null&&password.length()>0){
            member.setPassword(MD5Utils.md5(password));
        }
        memberDao.addMember(member);
    }

    @Override
    public List<Integer> findMemberCountBymonth(List<String> months) {
        List<Integer> monthCounts = new ArrayList<>();
        for (String month : months) {

            if (month !=null&&month.length()>0){
                Integer memberCount = memberDao.findMemberCountBymonth("%"+month+"%");
                monthCounts.add(memberCount);
            }
        }
        return monthCounts;
    }
}
